package com.manager.condition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;

import com.manager.Recorder.CustomerRecord;
import com.manager.condition.service.CustomerRecordService;
import com.manager.condition.service.CustomerService;
import com.manager.condition.service.TypeService;
import com.manager.condition.service.UserService;
import com.manager.model.Customer;
import com.manager.model.CustomerType;
import com.manager.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author WangLiang
* @description 针对表【customer】的数据库操作Service实现
* @createDate 2023-03-17 16:05:16
*/
@Service
public class CustomerRecordServiceImpl implements CustomerRecordService {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private UserService userService;
    @Autowired
    private TypeService typeService;
    @Override
    public IPage<CustomerRecord> getCustomerRecordPage(Page<Customer> page , QueryWrapper<Customer> wrapper) {
        List<CustomerRecord> customerRecordList = new ArrayList<>();//引入中间类customerRecordList
        CustomerRecord customerRecord = new CustomerRecord();//创建一个中间类对象customerRecord
        //调用方法实现分页查询
        IPage<Customer> pageCustomer = customerService.page(page, wrapper);//通过查customer表把一些基本信息取出
        List<Customer> customerList = pageCustomer.getRecords();//将这些基本信息放到customerList
        IPage<CustomerRecord> page1 = new Page<>();
        for (Customer customer:customerList) {
            Integer id = customer.getId();
            String name1 = customer.getName();//客户全名
            Integer sellerId = customer.getSellerId();
            String phone = customer.getPhone();
            String typeId = customer.getTypeId();
            Date preTime = customer.getPreTime();
            //取销售人员姓名
            User user = userService.getById(sellerId);
            String userName = user.getName();//销售人员姓名
            //取客户类别
            CustomerType customerType = typeService.getById(typeId);
            String typeName = customerType.getName();
            customerRecord = new CustomerRecord();//每次都指定不同的customerRecord
            customerRecord.setId(id);
            customerRecord.setName(name1);
            customerRecord.setPhone(phone);
            customerRecord.setSellerName(userName);
            customerRecord.setPreTime(preTime);
            customerRecord.setTypeName(typeName);
            customerRecordList.add(customerRecord);
        }
        long pages = pageCustomer.getPages();
        long current = pageCustomer.getCurrent();
        long size = pageCustomer.getSize();
        long total = pageCustomer.getTotal();
        page1.setPages(pages);
        page1.setCurrent(current);
        page1.setSize(size);
        page1.setTotal(total);
        page1.setRecords(customerRecordList);
        return page1;
    }
}




